﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.IO;
using System.Xml;

namespace TVC_WEB.Global
{
    public partial class OrderResult : System.Web.UI.Page
    {
        string sErr = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request["orderid"] != null)
                {
                    hdOrderId.Value = Request["orderid"].ToString();
                    //Request["orderid"] = null;
                    BUS.OrderControl odCtrl = new BUS.OrderControl();
                    DTO.OrderInfo odInfo = odCtrl.Get(Convert.ToInt32(hdOrderId.Value), ref sErr);
                    SetDataToForm(odInfo);
                    //    MessageBox.Show(RadGrid1.);
                    XmlDocument xmldoc = new XmlDocument();
                    xmldoc.Load(Server.MapPath("~/xmlCountry.xml"));
                    XmlElement doc = xmldoc.DocumentElement;
                    XmlElement eleBill = null;
                    XmlElement eleShip = null;
                    foreach (XmlElement ele in doc.ChildNodes)
                    {
                        if (ele.GetAttribute("id") == odInfo.Bill_Country)
                            eleBill = ele;
                        if (ele.GetAttribute("id") == odInfo.Ship_Country)
                            eleShip = ele;
                        if (eleBill != null && eleShip != null)
                            break;
                    }

                    StreamReader sr = new StreamReader(Server.MapPath("~/templatemail/OrderResult.htm"));
                    sr = File.OpenText(Server.MapPath("~/templatemail/OrderResult.htm"));
                    string content = sr.ReadToEnd();
                    sr.Close();
                    content = content.Replace("[BillAdd]", odInfo.Bill_Address);
                    content = content.Replace("[BillCity]", odInfo.Bill_City);
                    if (eleBill != null)
                        content = content.Replace("[BillCountry]", eleBill.GetAttribute("value").ToString());
                    else content = content.Replace("[BillCountry]", "");
                    //content = content.Replace("[BillEmail]", odInfo.Bill_Email);
                    content = content.Replace("[BillName]", odInfo.Bill_Name);
                    content = content.Replace("[BillPhone]", odInfo.Bill_Phone);
                    content = content.Replace("[OrderName]", odInfo.OrderProducts);

                    //BUS.PaymentControl payCtrl = new BUS.PaymentControl();
                    //DTO.PaymentInfo payInfo = payCtrl.Get_Payment(odInfo.IDPayment, ref sErr);
                    //content = content.Replace("[PayName]", payInfo.NameVN);

                    content = content.Replace("[ShipAdd]", odInfo.Ship_Address);
                    content = content.Replace("[ShipCity]", odInfo.Ship_City);
                    if (eleShip != null)
                        content = content.Replace("[ShipCountry]", eleShip.GetAttribute("value").ToString());
                    else content = content.Replace("[ShipCountry]", "");
                    //content = content.Replace("[ShipEmail]", odInfo.Ship_Email);
                    content = content.Replace("[ShipName]", odInfo.Ship_Name);
                    content = content.Replace("[ShipPhone]", odInfo.Ship_Phone);

                    BUS.OrderProductControl opCtrl = new BUS.OrderProductControl();
                    DataTable dt = opCtrl.GetByOrder(odInfo.OrderProducts, ref sErr);
                    dt.Columns.Add("SubTotal", typeof(double), "Price*Quantity");
                    content = content.Replace("[Count]", dt.Rows.Count.ToString("0"));
                    content = content.Replace("[TotalAmt]", Convert.ToDouble(dt.Compute("SUM(SubTotal)", "")).ToString("#,##0.##"));

                    try
                    {
                        Sendmail sm = new Sendmail();
                        BUS.UserControl uctr = new BUS.UserControl();
                        DTO.UserInfo uinf = uctr.Get(odInfo.UserID, ref sErr);
                        sm.SendMail("Đơn đặt hàng !!!",
                              content, uinf.Email, true, true);


                    }
                    catch (Exception ex)
                    {
                        Response.Write(ex.Message);
                    }
                }
                else
                    Response.Redirect("~/Default.aspx");
            }

        }

        private void SetDataToForm(DTO.OrderInfo odInfo)
        {
            lblBillAdd.Text = odInfo.Bill_Address;
            lblBillCity.Text = odInfo.Bill_City;
            lblBillCountry.Text = odInfo.Bill_Country;
            //lblBillEmail.Text = odInfo.Bill_Email;
            lblBillName.Text = odInfo.Bill_Name;
            lblBillPhone.Text = odInfo.Bill_Phone;
            lblOrder.Text = odInfo.OrderProducts;
            BUS.PaymentControl payCtrl = new BUS.PaymentControl();
            DTO.PaymentInfo payInfo = payCtrl.Get(odInfo.IDPayment, ref sErr);
            lblPayment.Text = payInfo.NameVN;
            lblVal.Text = payInfo.Value1.ToString("#,##0.##");
            lbShipAdd.Text = odInfo.Ship_Address;
            lbShipCity.Text = odInfo.Ship_City;
            lbShipCountry.Text = odInfo.Ship_Country;
            //lbShipEmail.Text = odInfo.Ship_Email;
            lbShipName.Text = odInfo.Ship_Name;
            lbShipPhone.Text = odInfo.Ship_Phone;
            BUS.OrderProductControl opCtrl = new BUS.OrderProductControl();
            DataTable dt = opCtrl.GetByOrder(odInfo.OrderProducts, ref sErr);
            dt.Columns.Add("SubTotal", typeof(double), "Price*Quantity");
            lvcart.DataSource = dt;
            lvcart.DataBind();
            //RadGrid1.DataSource = dt;
            //RadGrid1.Rebind();
            lblCount.Text = dt.Rows.Count.ToString("00");
            lblAmount.Text = (Convert.ToDouble(dt.Compute("SUM(SubTotal)", "")) + payInfo.Value1).ToString("#,##0.##");

        }
        public string GetPrice(string imageName)
        {
            if (imageName != "" && imageName != null)
                return Convert.ToDouble(imageName).ToString("N0") + "đ";
            else return "";
        }
        public string GetImage(string imageName)
        {
            string strUploadFolder = "/" + ConfigurationManager.AppSettings["UploadFolder"].ToString() + "Images/";
            return strUploadFolder + imageName;
        }
        protected void btnShop_Click(object sender, EventArgs e)
        {
            if (Session["currentshop"] != null)
                Response.Redirect(Session["currentshop"].ToString());
        }

        protected void lvcart_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
